import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Main2 {
    // 循环打印10次"ABC"
    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();
        Thread t1 = new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker1) {
                        locker1.wait();
                    }
                    System.out.print("A");
                    synchronized (locker2) {
                        locker2.notify();
                    }
                }
            } catch(InterruptedException e){
                    e.printStackTrace();
            }
        },"A");
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    synchronized (locker2) {
                        locker2.wait();
                    }
                    System.out.print("B");
                    synchronized (locker3){
                        locker3.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B");
        Thread t3 = new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker3) {
                        locker3.wait();
                    }
                    System.out.print("C\n");
                    synchronized (locker1) {
                        locker1.notify();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"C");
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);

        synchronized (locker1){
            locker1.notify();
        }
    }
}



class Main1 {
    // 按顺序打印三个线程名字
    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread("a"){
            @Override
            public void run() {
                try {
                    synchronized (locker2) {
                        locker2.wait();
                    }
                    System.out.println(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        };

        Thread t2 = new Thread("b"){
            @Override
            public void run(){
                try {
                    synchronized (locker1) {
                        locker1.wait();
                    }
                    System.out.println(Thread.currentThread().getName());
                    synchronized (locker2){
                        locker2.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread t3 = new Thread("c"){
            @Override
            public void run(){
                // 确保先执行a,b线程的wait
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
                synchronized (locker1) {
                    locker1.notify();
                }
            }
        };
        t1.start();
        t2.start();
        t3.start();
    }
}

// 创建线程的方式：工厂模式
public class Main {
    public static void main(String[] args) {
        ExecutorService pool1 = Executors.newCachedThreadPool();
        ExecutorService pool2 = Executors.newFixedThreadPool(10);
        ExecutorService pool3 = Executors.newScheduledThreadPool(5);
        ExecutorService pool4 = Executors.newSingleThreadExecutor();
    }
}
